//
//  Eyepiece+Filter.swift
//  WatermarkCamera
//
//  Created by AxcLogo on 4/7/2024.
//

import UIKit
import Foundation

extension Eyepiece {
    // MARK: - 滤镜方法

    // MARK: 增

    /// 追加滤镜
    /// - Parameter filter: 滤镜
    func appendFilter(_ filter: BaseFilter) {
        beginFilterResponseChain() // 准备开始

        filterList.append(filter)

        commitFilterResponseChain() // 提交更新
    }

    /// 插入滤镜
    /// - Parameters:
    ///   - filter: 滤镜
    ///   - index: 索引
    func insertFilter(_ filter: BaseFilter, at index: Int) {
        beginFilterResponseChain() // 准备开始

        filterList.insert(filter, at: index)

        commitFilterResponseChain() // 提交更新
    }

    // MARK: 删

    /// 移除滤镜
    /// - Parameter index: 索引
    func removeFilter(at index: Int) {
        beginFilterResponseChain() // 准备开始

        filterList.remove(at: index)

        commitFilterResponseChain() // 提交更新
    }

    /// 移除所有滤镜
    func removeAllFilter() {
        beginFilterResponseChain() // 准备开始

        filterList.removeAll()

        commitFilterResponseChain() // 提交更新
    }

    // MARK: 改

    /// 移动滤镜顺序
    /// - Parameters:
    ///   - index: 从第几个索引
    ///   - toIndex: 到第几个索引
    func moveFilter(form index: Int, to toIndex: Int) {
        beginFilterResponseChain() // 准备开始

        let formFilter = filterList[index]

        filterList.remove(at: index)

        filterList.insert(formFilter, at: toIndex)

        commitFilterResponseChain() // 提交更新
    }

    /// 修改滤镜
    /// - Parameters:
    ///   - filter: 滤镜
    ///   - index: 索引
    func replaceFilter(_ newFilter: BaseFilter, at index: Int) {
        beginFilterResponseChain() // 准备开始

        filterList[index] = newFilter

        commitFilterResponseChain() // 提交更新
    }

    /// 交换两个滤镜的顺序
    /// - Parameters:
    ///   - index: 从第几个索引
    ///   - toIndex: 到第几个索引
    func exchangeFilter(form index: Int, to toIndex: Int) {
        beginFilterResponseChain() // 准备开始

        let formFilter = filterList[index]

        let toFilter = filterList[toIndex]

        filterList[index] = toFilter

        filterList[toIndex] = formFilter

        commitFilterResponseChain() // 提交更新
    }

    /// 反转所有滤镜顺序
    func reversedAllFilter() {
        beginFilterResponseChain() // 准备开始

        filterList = filterList.reversed()

        commitFilterResponseChain() // 提交更新
    }

    // MARK: 查

    /// 获取滤镜
    /// - Parameter index: 索引
    /// - Returns: 滤镜
    func filter<FT: BaseFilter>(at index: Int) -> FT? {
        return filterList.axc.object(at: index) as? FT
    }

    /// 滤镜数量
    func filterCount() -> Int {
        return filterList.count
    }
}

extension Eyepiece {
    /// 准备开始刷新滤镜响应链
    func beginFilterResponseChain() {
        // 准备重新构造滤镜链、移除所有滤镜链

        for filter in filterList {
            filter.gpuFilter.removeAllTargets()
        }
    }

    /// 提交刷新滤镜响应链
    func commitFilterResponseChain() {
        if filterList.isEmpty { // 为空则首尾相连
            filterChain.header.removeAllTargets() // 移除头部

            filterChain.header.addTarget(filterChain.footer)

        } else {
            var lastFilter: BaseFilter?

            for filter in filterList {
                if let lastFilter = lastFilter {
                    lastFilter.gpuFilter.addTarget(filter.gpuFilter)
                }

                lastFilter = filter
            }

            guard let firstFilter = filterList.first,

                  let lastFilter = lastFilter else { return }

            setFilterHeader(firstFilter)

            setFilterFooter(lastFilter)
        }
    }

    /// 设置滤镜链头
    func setFilterHeader(_ filter: BaseFilter) {
        filterChain.header.removeAllTargets() // 移除头部

        filterChain.header.addTarget(filter.gpuFilter)
    }

    /// 设置滤镜链尾
    func setFilterFooter(_ filter: BaseFilter) {
        filter.gpuFilter.addTarget(filterChain.footer)
    }
}
